package com.dailyblue.project.estate.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.car.feign.OwnerFeign;
import com.dailyblue.project.estate.car.service.CarService;
import com.dailyblue.project.estate.commons.bean.Car;
import com.dailyblue.project.estate.commons.bean.Owner;
import com.dailyblue.project.estate.commons.handler.CarException;
import com.dailyblue.project.estate.commons.mapper.CarMapper;
import com.dailyblue.project.estate.commons.util.DateUtil;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {
    @Resource
    private CarMapper carMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private OwnerFeign ownerFeign;


    public JsonResult addCar(Car car) {
        boolean b = save(car);
        if (!b) {
           throw new CarException("汽车添加失败");
        }
        return ResultTool.success("success");
    }

    public JsonResult findOneCar(String carId) {
        Car car = getById(carId);
        return ResultTool.success(car);
    }

    //根据CarId查询Owner
    @Cacheable(cacheNames = "findAllCars")
    public JsonResult findAllCars() {
        List<Car> list = list();
        list.forEach(car -> {
            String ownerId = car.getOwnerId();
            JsonResult<Owner> owner = ownerFeign.getOwner(ownerId);
            car.setOwner(owner.getData());
        });
        return ResultTool.success(list);
    }

    @CacheEvict(cacheNames = "findAllCars",allEntries = true)
    public JsonResult deleteCar(String carId) {
        boolean b = removeById(carId);
        if (b) {
            return ResultTool.success("删除成功");
        }
        return ResultTool.fail(201,"删除失败");
    }

//    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "findAllCars",allEntries = true)
    public JsonResult updateCar(Car car) {
        UpdateWrapper<Car> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", car.getId());
        wrapper.set("picture", car.getPicture());
        wrapper.set("owner_id", car.getOwnerId());
        wrapper.set("color", car.getColor());
        wrapper.set("car_number", car.getCarNumber());
        wrapper.set("remark", car.getRemark());
        wrapper.set("update_time", DateUtil.nowDate());
        int i = carMapper.update(wrapper);
        if (i <= 0) {
            return ResultTool.fail(201, "更新失败");
        }
        return ResultTool.success("更新成功");
    }


}
